package logic

import (
	"context"

	"mail/service/property/rpc/internal/svc"
	"mail/service/property/rpc/property"
	"mail/service/property/model"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

type AlterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAlterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AlterLogic {
	return &AlterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AlterLogic) Alter(in *property.AlterRequest) (*property.AlterResponse, error) {
	// todo: add your logic here and delete this line
	res,err:=l.svcCtx.PropertyModel.FindOne(l.ctx,in.Id)
	if err!=nil {
		if err==model.ErrNotFound{
			return nil,status.Error(100,"property isn't exists.")
		}
		return nil,status.Error(500,err.Error())
	}
	if in.Name!="" {
		res.Name = in.Name
	}
	if in.Desc!="" {
		res.Desc = in.Desc
	}
	if in.Stock!=0 {
		res.Stock = in.Stock
	}
	if in.Amount!=0 {
		res.Amount = in.Amount
	}
	if in.Status!=0 {
		res.Status = in.Status
	}
	err = l.svcCtx.PropertyModel.Update(l.ctx,res)
	if err!=nil {
		return nil,status.Error(500,err.Error())
	} 
	return &property.AlterResponse{}, nil
}
